﻿//################################################################################################################################//
//##                                                                                                                            ##//    
//## This Page is implement for showing the doctor schedule after Doctor's login                                                ##//
//##                                                                                                                            ##//
//## Author   : Wahaj Bangash                                                                                                   ##//
//## Dated    : 04-04-2011                                                                                                      ##//
//##                                                                                                                            ##//    
//################################################################################################################################//
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.IO;
public partial class Doctor_DoctorSchedule : System.Web.UI.Page
{
    // creating an instance of manager class
    Manager mgr = new Manager(true,"ConnectionString");
    protected bool isFeatured = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        // COMMENTED TO AVOID LOGIN WHILE LOGGED IN AS DENTIST
        // DATE 15TH FEB 2008
        // if (Session["userID"] != null && Session["userID"].ToString() != "")
        localFunction obj = new localFunction();

        if (obj.checkDoctorLogin(true))
        // CODE MODIFICATION ENDS
        {
            if (!Page.IsPostBack)
            {
                //Fill Doctor Name and his/her speciality in your account box                
                ShowDrInformation();
            }
        }
        else
        {
            Response.Redirect(Session["strHttps"] + "doctor/doctorLogin.aspx");
        }

        Master.fVisible = true;

    }

    protected void ShowDrInformation()
    {
        //fetch doctor information using Session["userID"] from tbl_Doctors,tbl_Speciality
        string SQL = " SELECT  (tbl_Doctors.FName + ' ' + tbl_Doctors.MName  + ' ' + tbl_Doctors.LName) AS name,isnull(tbl_Speciality.Name,''), ";
        SQL += "isnull((select top 1 1 from tbl_bidding where trx_status=1 and dr_id=" + Session["userID"] + "),0) as isfeture ";
        SQL = SQL + " FROM ";
        SQL = SQL + " tbl_Doctors LEFT OUTER JOIN tbl_Speciality ON tbl_Doctors.SpecialityID = tbl_Speciality.Id";
        SQL = SQL + " WHERE tbl_Doctors.Id = " + Session["userID"];

        using (System.Data.SqlClient.SqlDataReader dr = mgr.ExecuteQuery(SQL))
        {
            if (dr.Read())
            {
                lblDoctorName.Text = dr.GetString(0);
                lblSpeciality.Text = dr.GetString(1);
                switch (dr.GetInt32(2))
                {
                    case 1:
                        //            isFeatured = true;
                        break;
                    default:
                        //            isFeatured = false;
                        break;
                }
            }
            dr.Close();
        }


    }

    protected void linkEditDrAccount_Click1(object sender, EventArgs e)
    {
        //Redirect page on doctorRegistration.aspx for edit the information of login doctor
        Session["pageFromOrAction"] = "Edit";
        Response.Redirect(Session["strHttps"].ToString() + "doctor/doctorRegistration.aspx");
    }

    protected void linkChangeDrPassword_Click1(object sender, EventArgs e)
    {
        //Redirect page on doctorForgotPass.aspx for retrive the forgot password
        //Response.Redirect("doctorChangePassword.aspx");
        Response.Redirect(Session["strHttp"].ToString() + "doctor/doctorChangePassword.aspx");
    }

    protected void Doctor_search1_Load(object sender, EventArgs e)
    {

    }
    protected void lnkViewProfile_Click(object sender, EventArgs e)
    {
        localFunction obj = new localFunction();
        obj.GetDrInfo(false);
        // Response.Redirect("DoctorsDetails.aspx?id=" + Session["userid"].ToString());             
    }
    protected void lnkbtnResource_Click(object sender, EventArgs e)
    {
        //Response.Redirect("doctorResources.aspx?id=" + Session["userid"].ToString());
        Response.Redirect(Session["strHttp"].ToString() + "doctor/doctorResources.aspx?id=" + Session["userid"].ToString());
    }
    protected void lnkCommunity_Click(object sender, EventArgs e)
    {
        Response.Redirect(Session["strHttp"].ToString() + "Community/AddUpdateBlog.aspx");
    }

    protected void lnkUpdateCalender_Click(object sender, EventArgs e)
    {
        string filename = Session["userid"].ToString();
        try
        {
            string path = Server.MapPath("~/Excel/");
            string strName;
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] finfo = di.GetFiles(filename + ".*");
            //finfo.
            //di.GetFiles(filename + ".*");

            //strName = System.IO.Path.GetFileName(path + filename);
            //FileInfo myFileInfo = new FileInfo(strName);
            //string extension = myFileInfo.Extension;
            string extension = finfo[0].Extension;
            strName = finfo[0].FullName;
            //string extension = System.IO.Path.GetExtension(strName).ToLower();
            switch (extension.ToLower())
            {
                case ".xlsx":
                    Response.ContentType = "application/vnd.ms-excel";
                    break;
                case ".xls":
                    Response.ContentType = "application/vnd.xls";
                    break;
            }

            //application/msexcel
            //  Response.ContentType = "application/vnd.ms-excel";//  "application/vnd.xls";
            //Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".xlsx");
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + extension);
            // Response.AppendHeader("Content-Length", filename.Length.ToString());

            //Response.TransmitFile(path + filename + ".xlsx");
            Response.TransmitFile(path + filename + extension);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //Response.Close();
            // Response.End();
            // Response.AddHeader("Content-Length", new System.IO.FileInfo("FileName.xlsx").Length)
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    protected void lnkUploadCalender_Click(object sender, EventArgs e)
    {
        UploadPanel.Visible = true;
        LabelPanel.Visible = true;
    }


    //protected void ButtonDownload_Click(object sender, EventArgs e)
    //{
    //    string filename = Session["userid"].ToString();

    //    string path = Server.MapPath("~/Excel/");

    //    Response.ContentType = "application/vnd.ms-excel";
    //    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
    //    Response.TransmitFile(path + filename + ".xls");

    //    //Response.Close();
    //    Response.End();

    //}
    protected void btnUploadFile_Click(object sender, EventArgs e)
    {
        string filename = Session["userid"].ToString();
        if (FileUploadExcel.HasFile)
        {
            string UploadFilename = System.IO.Path.GetFileNameWithoutExtension(FileUploadExcel.FileName);
            if (string.Compare(UploadFilename, filename, true) == 0)
            {
                string fileExt = System.IO.Path.GetExtension(FileUploadExcel.FileName);
                if (fileExt == ".xlsx")
                {
                    try
                    {
                        // alter path for your project
                        FileUploadExcel.SaveAs(Server.MapPath("~/Excel/" + FileUploadExcel.FileName));//filename + ".xlsx"));
                        LabelUpload.Text = "File " + FileUploadExcel.PostedFile.FileName + "Uploaded successfully";
                        UploadPanel.Visible = false;
                        LabelPanel.Visible = false;
                    }
                    catch (Exception ex)
                    {
                        //Response.Write(ex.Message);
                        LabelUpload.Text = "Error: " + ex.Message.ToString();
                    }
                }
                else
                {
                    LabelUpload.Text = "Only .xlsx files allowed!";
                }
            }
            else
            {
                LabelUpload.Text = "Filename doesnot match with your ID: " + filename + ".  Filename and your ID must be the same";
            }
        }
        else
        {
            LabelUpload.Text = "Please select a file to upload.";
        }
    }
}